<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\View;

use Lg\View\Asset;

/**
 * Description of JsBlock
 *
 * @author Allen <allen@lg4.cn>
 */
class JsBlock {

    public static $pos;

    public static function begin($pos = Asset::BODY_END) {
        static::$pos = $pos;
        ob_start();
    }

    public static function end() {
        $block = trim(ob_get_clean());
        $jsBlockPattern = '|^<script[^>]*>(?P<block_content>.+?)</script>$|is';
        if (preg_match($jsBlockPattern, $block, $matches)) {
            $block = $matches['block_content'];
        }
        Asset::addJsBlock($block, static::$pos);
    }

}
